<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row v-if="!shopId">
                    <el-col :span=6>
                        <el-form-item label="门店名称" prop="shopName">
                            <el-autocomplete
                                class="inline-input"
                                v-model="condition.search.shopName"
                                :fetch-suggestions="querySearch"
                                placeholder="门店名称"
                                :trigger-on-focus="false"
                                @select="handleSelect"
                            ></el-autocomplete>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="门店ID" prop="shopId">
                            <el-input v-model="condition.search.shopId"
                                      placeholder="门店ID"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="门店UUID" prop="shopUuid">
                            <el-input v-model="condition.search.shopUuid"
                                      placeholder="门店UUID"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="同步日期" prop="syncDate">
                            <el-date-picker
                                v-model="condition.search.syncDate"
                                type="date"
                                placeholder="同步日期"
                                value-format="yyyy-MM-dd"
                                align="right">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" v-on:click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item v-if="!shopId">
                            <el-button type="primary" v-on:click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="exportExcel">导出</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="rankShopTable"
            :data="tableData"
            size="small"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="60">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="rankName"
                label="榜单名称">
            </el-table-column>
            <el-table-column
                prop="shopName"
                width="100px"
                label="门店名称">
                <template v-slot="scope">
                    <el-link @click="clickShopName(scope.row)">
                        {{ scope.row.shopName }}
                    </el-link>
                </template>
            </el-table-column>
            <el-table-column
                prop="shopId"
                label="门店ID">
            </el-table-column>
            <el-table-column
                prop="shopCategoryName"
                label="门店分类">
            </el-table-column>
            <el-table-column
                prop="shopRankChanging"
                label="排名变化值">
            </el-table-column>
            <el-table-column
                prop="rankings"
                label="排名">
            </el-table-column>
            <el-table-column
                prop="fiveScore"
                label="星级">
            </el-table-column>
            <el-table-column
                prop="avgPrice"
                label="人均">
            </el-table-column>
            <el-table-column
                prop="favorNum"
                label="收藏量">
            </el-table-column>
            <el-table-column
                prop="recReason"
                label="推荐理由">
            </el-table-column>
            <el-table-column
                prop="syncDate"
                label="同步日期">
            </el-table-column>
            <el-table-column
                prop="createTime"
                label="创建时间">
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="50">
                <template v-slot="scope">
                    <div>
                        <el-button @click="addShop(scope.row)" type="text" size="small">添加</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>

        <el-dialog title="添加门店" width="30%" :visible.sync="addShopDialogVisible">
            <el-form :model="addShopForm" label-width="120px" :size="size">
                <el-form-item label="门店UUID">
                    <el-input v-model="addShopForm.shopUuid" disabled placeholder="门店UUID"></el-input>
                </el-form-item>
                <el-form-item label="门店名称">
                    <el-input v-model="addShopForm.shopName" disabled placeholder="门店名称"></el-input>
                </el-form-item>
                <el-form-item label="是否更新">
                    <el-switch v-model="addShopForm.status"></el-switch>
                </el-form-item>
                <el-form-item label="同步CPC">
                    <el-switch v-model="addShopForm.syncCpcStatus"></el-switch>
                </el-form-item>
                <el-form-item label="是否收藏">
                    <el-switch v-model="addShopForm.favoriteFlag"></el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addShopDialogVisible = false" :size="size">取 消</el-button>
                <el-button @click="addShopConfirm" type="primary" :size="size">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addDpRankShop,
    addDpShop,
    deleteDpRankShop,
    getDpRankShopByPage,
    selectDpRankShopByQ,
    updateDpRankShop
} from '@/api/api'

export default {
    name: "DpRankShop",
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {},

            // 添加门店
            addShopDialogVisible: false,
            addShopForm: {},
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        rankId: String,
        shopId: String,
        shopName: String,
        size: {
            type: String,
            default: 'small'
        },
    },
    watch: {
        shopName: function (val, oldVal) {
            this.shopName = val;
        }
    },
    methods: {
        // 远程搜索
        querySearch(queryString, cb) {
            selectDpRankShopByQ({'q': queryString}).then(data => {
                let tt = data.body;
                let values = [];
                tt.forEach((item, index, arr) => {
                    values.push({"value": item});
                })
                // 调用 callback 返回建议列表的数据
                cb(values);
            })
        },
        // 添加门店
        addShop(row) {
            this.addShopForm.shopUuid = row.shopUuid;
            this.addShopForm.shopName = row.shopName;
            this.$set(this.addShopForm, "status", false);
            this.$set(this.addShopForm, "syncCpcStatus", false);
            this.$set(this.addShopForm, "favoriteFlag", false);
            this.addShopDialogVisible = true;
        },
        // 添加门店确定
        addShopConfirm: function () {
            addDpShop(this.addShopForm).then(data => {
                if (data.respCode == 1000) {
                    this.addShopDialogVisible = false;
                }
            });
        },
        handleSelect(item) {
            console.log(item);
        },
        clickShopName(row) {
            let url = "https://m.dianping.com/shopshare/" + row.shopUuid + "?msource=Appshare2021&utm_source=shop_share"
            window.open('javascript:window.name;', '<script>location.replace("' + url + '")<\/script>');
        },
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },

        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            let name = "榜单收藏数据";
            if (this.shopName) {
                name = this.shopName + name;
            }
            this.GLOBAL.exportExcel('#rankShopTable', name + '.xlsx');
        },
        add: function () {
            addDpRankShop(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        update: function () {
            updateDpRankShop(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteDpRankShop({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取表格数据
        getData: function () {
            if (this.rankId) {
                this.condition.search.rankId = this.rankId;
            }
            if (this.shopId) {
                this.condition.search.shopId = this.shopId;
            }
            getDpRankShopByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
            });
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
